package top.songxs.yygh.hosp.listener;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.songxs.mq.consts.MqConst;
import top.songxs.mq.service.RabbitService;
import top.songxs.yygh.hosp.service.ScheduleService;
import top.songxs.yygh.model.hosp.Schedule;
import top.songxs.yygh.vo.order.OrderMqVo;

@Component
public class ListenerOrderQueue {


    @Autowired
    ScheduleService scheduleService;

    @Autowired
    RabbitService rabbitService;

    @RabbitListener(
            bindings = {
                    @QueueBinding(
                            value = @Queue(durable = "true", name = MqConst.QUEUE_ORDER),
                            exchange = @Exchange(name = MqConst.EXCHANGE_DIRECT_ORDER, type = "direct"),
                            key = {MqConst.ROUTING_ORDER}
                    )
            }
    )
    public void listener(OrderMqVo orderMqVo) {

        if (null != orderMqVo.getAvailableNumber()){


            //将mg中该排班的两个num更新成医院端返回的两个num
            Schedule schedule = scheduleService.getScheduleById(orderMqVo.getScheduleId());
            schedule.setReservedNumber(orderMqVo.getReservedNumber());
            schedule.setAvailableNumber(orderMqVo.getAvailableNumber());
            scheduleService.update(schedule);
            System.out.println(schedule+"创建订单");
            //说明是创建订单
        }else {
            //说明是取消订单
            //2、num为空说明是取消订单
            //将mg中该排班的availableNumber+1
            Schedule schedule = scheduleService.getScheduleById(orderMqVo.getScheduleId());
            int availableNumber = schedule.getAvailableNumber().intValue() + 1;
            schedule.setAvailableNumber(availableNumber);
            scheduleService.update(schedule);
            System.out.println(schedule+"取消订单");
        }

        rabbitService.sendMessage(MqConst.EXCHANGE_DIRECT_MSM, MqConst.ROUTING_MSM_ITEM, orderMqVo.getMsmVo());






    }


}

